//
//  HLHomeViewModel.swift
//  SwiftPods
//
//  Created by hulinhu on 2018/1/16.
//  Copyright © 2018年 hulinhu. All rights reserved.
//

import UIKit
import Moya
import RxSwift
import RxCocoa
import Alamofire
import ObjectMapper
import Kingfisher

enum HLRefreshStatus {
    case none
    case beginHeaderRefresh
    case endHeaderRefresh
    case beginFooterRefresh
    case endFooterRefresh
    case noMoreData
}
protocol OutputRefreshProtocol {

    var refreshStatus : Variable<HLRefreshStatus> {get}
}

class HLHomeViewModel: NSObject {
    
    var bag : DisposeBag = DisposeBag()
    
    // 创建信号量网络请求
    let requestHomeDataCommond = PublishSubject<Bool>()

    // 网络请求类
    let provider = HLNetWorkProvider<HLHomeNetManager>()
    
    // 网络请求页数
    var pageIndex = Int()
    
    // tableView
    var tableView = UITableView()
    
    // 数据监听
    var modelObserable = Variable<[StoryModel]> ([])
    
    typealias Input = HCRecommendInput
    typealias Output = HCRecommendOutput
    
    struct HCRecommendInput {
    }
    
    struct HCRecommendOutput: OutputRefreshProtocol {
        var refreshStatus = Variable<HLRefreshStatus>(.none)
    }
    
//    func transform(input: HLHomeViewModel.HCRecommendInput) -> HLHomeViewModel.HCRecommendOutput {
//
//    }
    
    var refreshStateObserable = Variable<HLRefreshStatus>(.none)

    
    
    // 刷新监听

    func setupConfig() {
        
         //MARK: Rx 绑定tableView数据
        modelObserable.asObservable().bind(to: tableView.rx.items(cellIdentifier: cellID, cellType:HLHomeTableViewCell.self)){row,model,cell in
            cell.titleLabel.text = model.title
            cell.ImageV?.kf.setImage(with: URL.init(string: (model.images?.count)! > 0 ? (model.images?.first)! :""))

        }.disposed(by: bag)
        

        requestHomeDataCommond.subscribe { (event : Event<Bool>) in
            if event.element!{
                self.pageIndex = 0
                self.provider.request(.GETHomeList).filterSuccessfulStatusCodes().mapJSON().mapObject(type: HomeModel.self).subscribe(onNext: { [weak self] (model) in
                    guard let `self` = self else { return }
                   self.modelObserable.value = model.stories!
                   self.refreshStateObserable.value = .endHeaderRefresh

                }).disposed(by: self.bag)
            }else{
                self.pageIndex += 1
                self.provider.request(.GETHomeList).filterSuccessfulStatusCodes().mapJSON().mapObject(type: HomeModel.self).subscribe(onNext: { [weak self] (model) in
                    guard let `self` = self else { return }
                    self.modelObserable.value += model.stories!
                    self.refreshStateObserable.value = self.pageIndex > 3 ? .noMoreData : .endFooterRefresh
                    
                }).disposed(by: self.bag)
            }

        }.disposed(by: self.bag)
        
        
        refreshStateObserable.asObservable().subscribe(onNext: { (events) in
            switch events{
            case .beginHeaderRefresh:
                self.tableView.mj_header.beginRefreshing()
            case .endHeaderRefresh:
                self.tableView.mj_header.endRefreshing()
                self.tableView.mj_footer.resetNoMoreData()
            case .beginFooterRefresh:
                self.tableView.mj_footer.beginRefreshing()
            case .endFooterRefresh:
                self.tableView.mj_footer.endRefreshing()
            case .noMoreData:
                self.tableView.mj_footer.endRefreshingWithNoMoreData()
            default:
                break
            }
            
        }).disposed(by: bag)
        
    }
}
